<?php
namespace addons\unishop\controller;
use addons\unishop\extend\Hashids;
use addons\unishop\model\Area;
use addons\unishop\model\Config;
use addons\unishop\model\Evaluate;
use addons\unishop\model\Product;
use app\admin\model\unishop\Coupon as CouponModel;
use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
use addons\unishop\model\OrderRefund;
use app\admin\model\unishop\OrderRefundProduct;
use think\Db;
use think\Exception;
use addons\unishop\model\Address as AddressModel;
use think\Hook;
use think\Loader;

/**
 * 订单
 */
class Money extends Base
{

    /**
     * 允许频繁访问的接口
     * @var array
     */
    protected $frequently = [''];

    protected $noNeedLogin = [''];

    public function getmymoney()
    {

        $userinfo = $this->auth->getUser();

        $user=Db::name('user')->where('id',$userinfo['id'])->find();




        if ($user['money']<1){
            $this->error('余额不足');
        }

        if ($user['viptime'] < time()){
            $this->error('权限不够，请购买商品增加权限');
        }

        $tx['user_id']=$user['id'];
        $tx['money']=$user['money'];
        $tx['createtime']=time();
        Db::name('user_money_tx')->insert($tx);


        setmymoney($user['id'],$user['money'],'用户提现');

        $this->success('',['code'=>1,'msg'=>'ok']);



    }

    /**
     * 余额记录
     */

    public function getmymoneylog()
    {

        $userinfo = $this->auth->getUserInfo();
        $types=$this->request->param('types');


        $map['user_id']=['eq',$userinfo['id']];

        if ($types=='income'){
            $map['type']=['eq',$types];
        }else if ($types=='expense'){
            $map['type']=['eq',$types];
        }


        $lists=Db::name('user_money_log')
            ->where($map)
            ->order('id desc')
            ->select();


        foreach ($lists as $key=>$value){
            $lists[$key]['createtime']=date('Y-m-d H:i:s',$value['createtime']);
        }

        $this->success('', $lists);



    }

    /**
     * 提现记录
     */

    public function getwxshoukuan()
    {

        $uid=$this->auth->id;

        $lists=Db::name('user_wxmoney')
            ->where(['user_id'=>$uid])
            ->where(['status'=>0])
            ->order('id desc')
            ->find();

        $this->success('', $lists);

    }

    public function setwxshoukuan()
    {
        $uid=$this->auth->id;
        $id=$this->request->param('id');


        $wx=Db::name('user_wxmoney')
            ->where(['id'=>$id])
            ->update(['status'=>1]);

        $this->success('', $wx);
    }

}